package cn.itlight.pet.controller;

import cn.itlight.basic.exception.BusinessException;
import cn.itlight.basic.utils.LoginContext;
import cn.itlight.org.domain.Employee;
import cn.itlight.org.service.IEmployeeService;
import cn.itlight.pet.domain.Pet;
import cn.itlight.pet.dto.HandlerMsgDto;
import cn.itlight.pet.service.ISearchMasterMsgService;
import cn.itlight.pet.domain.SearchMasterMsg;
import cn.itlight.pet.query.SearchMasterMsgQuery;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.PageList;
import cn.itlight.user.domain.Logininfo;
import cn.itlight.user.domain.User;
import cn.itlight.user.service.IUserService;
import cn.itlight.user.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    /*
    * @Title: publish
    * @Description: 寻主新增
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/19 16:17
    * @Parameters:
    * @Return
    */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest req){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(req);
            // 1.根据LogininfoId查询用户
            User user = userService.loadByLogininfoId(logininfo.getId());
            // 设置上传用户id
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /* 
    * @Title: toAudit
    * @Description: 查询待审核
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/20 11:06  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }


    /*
    * @Title: toHandle
    * @Description: 查询待处理
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/20 11:07
    * @Parameters:
    * @Return
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest req){
        // 1.设置寻主消息状态为待审核，设置店铺Id
        query.setState(1L);
        Logininfo logininfo = LoginContext.getLogininfo(req);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryData(query);
    }    
    
    /* 
    * @Title: pool
    * @Description: 查询寻主池
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/20 11:08  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    /*
    * @Title: finish
    * @Description: 查询已完成的寻主
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/20 11:09
    * @Parameters:
    * @Return
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }


    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto){
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.me();
        }catch(BusinessException e){
            e.printStackTrace();
            // 如果是自定义异常，那么返回给前端的提示语就是service层设置的提示语
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！！！");
        }
    }

    @PostMapping(value="/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }

}
